const parseHtml = res => {
  var html = res.data;
  var start = html.indexOf("class='project-list'");
  var end = html.indexOf("class='search-message'");

  html = html.substring(start - 5, end - 5);

  //把QQ群过滤掉。
  html = html.replace(/QQ([\s\S]*?)[0-9]{6,12}/g, "");
  html = html.replace(/qq([\s\S]*?)[0-9]{6,12}/g, "");
  html = html.replace(/[0-9]{6,12}/g, "");

  //抓取标题数组
  var titlelArr = html.match(/<div[ \n]*class='project-title'>([\s\S]*?)<\/div>/g);
  if (null == titlelArr || titlelArr.length==0)
  {
    return [];
  }

  //处理标题数组
  titlelArr = titlelArr.map(function (item) {
    item = item.replace(/<\/?.+?>/g, "");
    item = item.replace(/\s+/g, "");
    return item;
  });


  //抓取url数组
  var urlArr = html.match(/<a href=([\s\S]*?)class=\"project\">/g);
  urlArr = urlArr.map(function (item) {
    item = item.replace("<a href=\"", "");
    item = item.replace("class=\"project\">", "");
    item = item.replace("\"", "");
    item = item.replace(/\s+/g, "");
    return item;
  });


  //抓取简介数组
  var summaryArr = html.match(/<div class='project-description'>([\s\S]*?)<\/div>/g);
  summaryArr = summaryArr.map(function (item) {

    item = item.replace(/<\/?.+?>/g, "");
    item = item.replace(/\s+/g, "");
    return item;
  });


  //抓取star数组
  var starArr = html.match(/<i[ \n]*class='iconfont icon-star'>([\s\S]*?)<i[ \n]*class='iconfont icon-fork'>/g);
  starArr = starArr.map(function (item) {

    item = item.replace(/<\/?.+?>/g, "");
    item = item.replace(/\s+/g, "");
    
    return item;
  });

  //抓取lable数组
  var lableArr = html.match(/<div class='project-tags'>([\s\S]*?)<\/div>[ \n]*<\/div>/g);
  if (null == lableArr)
  {
    lableArr = [];
  }
  lableArr = lableArr.map(function (item) {
    item = item.replace(/<\/div>/g, ",");
    return item;
  });

  //剔除html和空格
  lableArr = lableArr.map(function (item) {
    item = item.replace(/<\/?.+?>/g, "");
    item = item.replace(/\s+/g, "");
    return item;
  });

  //重构lable数组
  lableArr = lableArr.map(function (item) {
    var arrTemp = item.split(",");
    if (null != arrTemp)
    {
      arrTemp = arrTemp.slice(0,2);
    }
    return arrTemp;
  });

  var resultList = new Array();
  for (var i = 0; i < titlelArr.length;i++)
  {
    var project = {};
    project.headImg = [];
    project.title = titlelArr[i];
    project.summary = summaryArr[i];

    project.icon = "/static/icon/gitee.jpg";
    project.url = "https://m.gitee.com"+urlArr[i];
    project.star = starArr[i];
    project.lableArr = lableArr[i];

    resultList.push(project);
  }
  return resultList;
}

//过滤出图片
const filterImg = html => {
  var imgArr = html.match(/<img src=\"([\s\S]*?)\"/g);
  if(null == imgArr)
  {
    return [];
  }
  imgArr = imgArr.map(function (item) {
    item = item.replace("<img src=\"", "");
    item = item.replace("\"", "");
    return item;
  });

  var imgArray = [];
  for (var i = 0; i < imgArr.length;i++)
  {

    // if(imgArr[i].startsWith("https://m.gitee.com"))
    // {
    //   imgArray.push(imgArr[i]);
    //   continue;
    // }

    //只把相对路径显示的抽出来，在最后显示
    if (imgArr[i].startsWith("/"))
    {
      imgArray.push("https://m.gitee.com" + imgArr[i]);
    }
  }
  return imgArray;
}

module.exports = {
  parseHtml: parseHtml,
  filterImg
}